package com.jdpc.weparty.security

import com.jdpc.weparty.entity.Role
import com.jdpc.weparty.entity.User
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority

object JwtUserFactory {
    /**
     * 创建JwtUser实例。
     * @param user 该参数为通过用户名从数据库中查找到的user实例
     * @param roles 该参数为user对应的身份列表
     */
    fun create(user: User, roles: List<Role>): JwtUser {
        return JwtUser(
                user.id,
                user.phoneNumber,
                user.password,
                mapToGrantedAuthorities(roles),
                user.lastPasswordResetDate,
                user.wxOpenId
        )
    }

    private fun mapToGrantedAuthorities(authorities: List<Role>): List<GrantedAuthority> {
        return authorities.map {
            SimpleGrantedAuthority(it.name.toString())
        }

    }
}